/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 * Description: implement the cast source connect
 * Author: linweidong
 * Create: 2024-08-20
 */

#ifndef CAST_ABILITY_HELPER_H
#define CAST_ABILITY_HELPER_H

#include "cast_engine_log.h"
#include "cast_engine_errors.h"
#include "extension_manager_client.h"
#include "want.h"

namespace OHOS {
namespace CastEngine {
namespace CastEngineService {
class CastAbilityHelper {
    DEFINE_CAST_ENGINE_LABEL("Cast-Ability-Helper");
public:
    CastAbilityHelper() = default;
    ~CastAbilityHelper() = default;

    /*
     * 连接ServiceExtensionAbility
     *
     * @param bundleName 包名
     * @param abilityName ability名称
     * @param connect 远程连接
     * @return 连接结果
     */
    static int ConnectServiceExtensionAbility(
        const std::string bundleName, const std::string abilityName, const sptr<IRemoteObject> &connect)
    {
        AAFwk::Want want;
        want.SetAction("");
        want.SetElementName(bundleName, abilityName);

        int ret = AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want, connect, -1);
        if (ret != CAST_ENGINE_SUCCESS) {
            CLOGE("connect ability fail, error code: %{public}d", ret);
            return CAST_ENGINE_ERROR;
        }
        return CAST_ENGINE_SUCCESS;
    }

    /*
     * 断连ServiceExtensionAbility
     *
     * @param connect 远程连接
     * @return 断连结果
     */
    static int DisconnectServiceExtensionAbility(const sptr<IRemoteObject> &connect)
    {
        int ret = AAFwk::ExtensionManagerClient::GetInstance().DisconnectAbility(connect);
        if (ret != CAST_ENGINE_SUCCESS) {
            CLOGE("disconnect ability fail, error code: %{public}d", ret);
            return CAST_ENGINE_ERROR;
        }
        return CAST_ENGINE_SUCCESS;
    }
};
} // namespace CastEngineService
} // namespace CastEngine
} // namespace OHOS
#endif  // CAST_ABILITY_HELPER_H